/**
 *  用于按顺序载入js和css文件
 * @date 2020-08-22
 * @param {Array} urlArray 输入 js文件或css文件的URL地址数组
 */
async function load(urlArray, type = "sync") {
    if (type === "sync") {
        //同步加载
        await Promise.all(urlArray.map((url) => Script(url)));
    } else if (type === "async") {
        //异步加载
        for (let i = urlArray.length; i--; ) {
            await Script(urlArray[i]);
        }
    }
    console.log("%c 全部载入成功", "color:green");
}

/**
 * 用于将 url 导入到 body 中
 * @date 2020-08-22
 * @param {String} url js 或 css 的URL
 * @returns {Promise}
 */
function Script(url) {
    return new Promise((res) => {
        let a = {};
        if (/.js$/.test(url)) {
            a = document.createElement("script");
            a.src = url;
        } else if (/.css$/.test(url)) {
            a = document.createElement("link");
            a.href = url;
            a.rel = "stylesheet";
            a.type = "text/css";
        }

        a.onload = () => {
            console.log(url + "载入成功");
            res(true);
        };
        document.body.appendChild(a);
    });
}
module.exports = load;
